package com.kyleromano.disgaea.sql;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class CharacterDataSource {
	private SQLiteDatabase db;
	private DBHelper dbHelper;

	/**
	 * Construtor
	 * @param context
	 */
	public CharacterDataSource(Context context)
	{
		dbHelper = new DBHelper(context);
	}
	
	/**
	 * Open a connection to the Database
	 * @throws SQLException
	 */
	public void open() throws SQLException
	{
		db = dbHelper.getWritableDatabase();
	}
	
	/**
	 * Close the connection the database
	 */
	public void close()
	{
		dbHelper.close();
	}
	
	public boolean isOpen() {
		return dbHelper.isOpen();
	}
	
	/**
	 * Retrieve a cursor containing all characters in the database
	 * @return
	 */
	public Cursor getAllCharacters(String orderBy, boolean descendingOrder)
	{
		Cursor c = null;
		String select = "SELECT * FROM " + DBConstants.VCHARACTER;
		if(orderBy != null && orderBy.trim().length() > 0)
			select += " ORDER BY " + orderBy;
		else
			select += " ORDER BY " + DBConstants.CCID;
		if(!descendingOrder)
			select += " ASC";
		else
			select += " DESC";
		c = db.rawQuery(select, null);
		return c;
	}
	
	/**
	 * Retrieve a cursor object containing a single Character from the database
	 * @param id
	 * @return
	 */
	public Cursor getOneCharacter(int id)
	{
		Cursor c = null;
		if(db.isOpen())
			c = db.rawQuery("SELECT * FROM " +DBConstants.VCHARACTER+ " WHERE "+DBConstants.CCID+ "=?", new String[]{id+""});
		return c;
	}
	
	public Cursor select(String select, String[] selectionArgs)
	{
		Cursor c = null;
		if(db.isOpen())
			c = db.rawQuery(select, selectionArgs);
		return c;
	}
	
	/**
	 * Insert a new record into the database
	 * @param table Table to insert the record into
	 * @param column Presumably the PK of the table?
	 * @param cv the values to insert
	 */
	public boolean insert(String table, String column, ContentValues cv) {
		long l = db.insert(table, column, cv);
		if(l != -1)
			return true;
		else
			return false;
	}
	
	/**
	 * Return the number of Characters in the database
	 */
	public int getCount()
	{
		Cursor c = null;
		int count = 0;
		if(db.isOpen())
			c = db.rawQuery("SELECT COUNT("+DBConstants.CCID+") FROM " +DBConstants.VCHARACTER, null);
		if(c != null){
			c.moveToFirst();
			count = c.getInt(0);
			c.close();
		}
		return count;
	}
	
	/**
	 * Return the last character ID
	 */
	public int getLastID()
	{
		Cursor c = null;
		int id = 0;
		if(db.isOpen())
			c= db.rawQuery("SELECT MAX ("+DBConstants.CCID+") FROM " +DBConstants.VCHARACTER, null);
		if (c!= null && c.getCount() > 0)
		{
			c.moveToFirst();
			id = c.getInt(0);
		}
		if(c!=null)
			c.close();
		return id;
	}
	
	/**
	 * Select all class IDs where the unlocked value is 1 (true)
	 * @return Cursor at the first row from the database or null if nothing found
	 */
	public Cursor getUnlockedClasses()
	{
		Cursor c = null;
		if(db.isOpen())
			c=db.rawQuery("SELECT _id FROM " +DBConstants.VCLASS+ " WHERE " +DBConstants.CCLASSUNLOCKED+ "=1", null);
		if(c!=null && c.getCount() > 0)
			return c;
		else
			return null;
	}
	
	/**
	 * Passthru to database.update
	 * @param table
	 * @param values
	 * @param whereClause
	 * @param whereArgs
	 * @return True if at least one row was updated
	 */
	public boolean update(String table, ContentValues values, String whereClause, String[] whereArgs)
	{
		int i = db.update(table, values, whereClause, whereArgs);
		if(i > 0)
			return true;
		else
			return false;
	}

	public boolean deleteRecord(String table, String column, String where) {
		int i = db.delete(table, column + "=?", new String[] { where });
		if(i > 0)
			return true;
		else
			return false;
	}
}
